###############################################################################
#                                                                             #
#    This file is part of the testsuite of the "ssbot" Python module.         #
#    It is distributed under the MIT License. See LICENSE.txt for details.    #
#                                                                             #
###############################################################################


import multiprocessing
import sys
import unittest
import xmlrpclib

sys.path.append('../')

import ssbot.schedulers


class CommandListenerTests(unittest.TestCase):
    def test_command_relay(self):
        port = 8011
        cmd_queue = multiprocessing.Queue()
        p = multiprocessing.Process(
            target=ssbot.schedulers.CommandListener(port, cmd_queue))
        p.start()
        s = xmlrpclib.ServerProxy('http://localhost:%d' % port)
        try:
            s.run_cmd({ 'type': ssbot.schedulers.STOP_COMMAND })
            cmd = cmd_queue.get()
            p.terminate()
        except:
            p.terminate()
            self.fail('Unable to send/receive command.')
        self.assertEqual(cmd['type'], ssbot.schedulers.STOP_COMMAND)


if __name__ == '__main__':
    unittest.main()
